﻿<link href="@Url.Content("/content/styles/order.css")" rel="stylesheet" type="text/css" />

<div class="ld-tabs">
    <ul>
        <li class="k-state-active">填写新的 @Invoicing.Web.Management.Common.GetOrderName(ViewBag.Method)</li>
    </ul>
    <div>
        <div class="k-block form_title">
            <div>
                <b>日期：</b><input id="txtNoteDate" />
                <b>客户：</b><input id="txtCustomer" data-value="0" style="width: 300px;" />
                <button class="k-button" onclick="GetDealOrders()"><span class="k-icon k-i-search"></span><span>搜索交易单据</span></button>
            </div>
        </div>
        <table>
            <tr style="vertical-align:top">
                <td>
                    <div id="grid_deal_orders"></div>
                </td>
                <td>
                    <div id="grid_orders"></div>
                    <table class="tbsum">
                        <tr>
                            <td colspan="3" style="font-size:13px">
                                <b>备注：</b><input class="k-textbox" id="txtDescription" style="width:80%" />
                            </td>
                        </tr>
                        <tr>
                            <td style="width: 296px"></td>
                            <td style="width: 295px;">&nbsp;&nbsp;总金额：<span id="lblSum"></span></td>
                            <td></td>
                        </tr>
                    </table>
                </td>
            </tr>
        </table>

    </div>
</div>

<script class="init">
    var submenu;
    @if (ViewBag.Method == 21)
    {
        <text>submenu = "付款单";</text>
    }
    else if (ViewBag.Method == 22)
    {
        <text>submenu = "收款单";</text>
    }
    LocatedMenu("财务管理", submenu);

    $("#txtNoteDate").kendoDatePicker({
        value: new Date(),
        culture: "zh-CHS",
        format: "yyyy年MM月dd日",
    });

    var allCustomer = Whale.Remote.GetEntity({
        url: "/Members/Customer/Gets",
        parameters: {
            cType: "@((ViewBag.Method == (int)Invoicing.Model.Orders.OrderMethod.FK) ? 2 : 1)",
            customerStatus: 1
        }
    });
    $("#txtCustomer").kendoComboBox({
        placeholder: "请选择客户...",
        filter: "contains",
        dataTextField: "SearchLabel",
        template: "#=ComplexName#",
        dataValueField: "ID",
        dataSource: allCustomer,
        select: function (e) {
            var item = this.dataItem(e.item.index());
            $("#txtCustomer").attr("data-value", item.ID);
        },
        change: function (e) {
            var item = this.dataItem();
            if (item != undefined) {
                this.text(item.ComplexName);
            } else {
                var b = true;
                var obj = this;
                $.each(allCustomer, function (i, o) {
                    if (obj.text() == o.ComplexName) {
                        b = false;
                    }
                });
                if (b) {
                    this.text("");
                    $("#txtCustomer").attr("data-value", "0");
                }
            }
        }
    });
</script>

<script class="grid_deal">
    var dealOrdersFields = [{
        title: "<input type='checkbox' onclick='SelectAll()' />",
        template: "#=CheckBox(OrderNo)#",
        width: 30,
        attributes: { style: "text-align:center" }
    }, {
        title: "单据号",
        field: "OrderNo",
        template: "#=GetOperations(OrderNo)#",
        width: 120
    }, {
        title: "总金额",
        field: "TotalMoney",
        format: "{0:c}",
        width: 100,
        attributes: { style: "text-align:right" }
    }, {
        title: "已支付",
        field: "PaidMoney",
        format: "{0:c}",
        width: 100,
        attributes: { style: "text-align:right" }
    }, {
        title: "状态",
        field: "OrderStatus",
        template: "#=GetOrderStatusString(OrderStatus)#",
        width: 100
    }, {
        title: "日期",
        template: "#=kendo.toString(kendo.parseDate(NoteDate),'yyyy-MM-dd')#",
        width: 100
    }, {}];

    function GetOperations(orderNo) {
        var html = "<a href=\"javascript:ShowBuyOrder('" + orderNo + "')\">" + orderNo + "</a>";
        return html;
    }
    function ShowBuyOrder(orderNo) {
        if (wndGeneralEdit != null && typeof wndGeneralEdit != "undefined") {
            wndGeneralEdit.setOptions({ title: "单据详情", width: 900 });
            wndGeneralEdit.refresh({ url: "/Orders/DealOrder/Order/" + orderNo });
            wndGeneralEdit.open();
        }
    }
    function CheckBox(orderNo) {
        var html = "<input type='checkbox' data-value='" + orderNo + "' />";
        return html;
    }
    function SelectAll() {
        var obj = $("#grid_deal_orders table tr th input[type='checkbox']")
        $("#grid_deal_orders table td input[type='checkbox']").prop("checked", obj.is(":checked"));
    }

    $(function () {
        $("#grid_deal_orders").kendoGrid({
            columns: dealOrdersFields,
            dataSource: Whale.dataSource({
                url: "/Orders/DealOrder/GetOrders",
                getParameters: function () {
                    return {
                        customerID: $("#txtCustomer").attr("data-value"),
                        orderMethod: "@(ViewBag.Method==21?"1,4":"2,3")",
                        orderStatus: "5,8"
                    };
                },
                pageSize: 0
            }),
            dataBound: function (e) {
                //让表头居中
                $("th.k-header").attr("style", "text-align:center; font-weight:bold;");
            },
            autoBind:false
        });
    });
    function GetDealOrders() {
        if ($("#txtCustomer").attr("data-value") != "0") {
            $("#grid_deal_orders table tr th input[type='checkbox']").prop("checked", false);
            $("#grid_deal_orders").data("kendoGrid").dataSource.read();
        }
    }
</script>

<script class="grid_orders_columns">
    var orderFields = [{
        title: "往来帐户",
        field: "Bank",
        template: "#=Bank.ShowName#",
        editor: BankEditor,
        width: 300
    }, {
        title: "金额",
        field: "Money",
        format: "{0:c}",
        editor: moneyEditor,
        width: 150
    }, {
        title: "操作",
        command: [{
            name: "destroy",
            text: "删除"
        }]
    }];

    function BankEditor(container, options) {
        var selBank = $('<input required data-text-field="ShowName" data-value-field="ID" data-bind="value:' + options.field + '"/>')
            .appendTo(container)
            .kendoDropDownList({
                autoBind: false,
                dataSource: Whale.dataSource({
                    url: "/Financial/Bank/Gets"
                }),
                change: function (e) {
                    if (selBank.dataItem() == null || selBank.dataItem() == undefined) {
                        $("#grid_orders").data("kendoGrid").cancelRow();
                        return;
                    }
                    options.model.Bank = selBank.dataItem();
                },
                dataBound: function (e) {
                    if (selBank.dataItem(0) != null)
                        options.model.Bank = selBank.dataItem(0);
                }
            }).data("kendoDropDownList");
    }

    function moneyEditor(container, options) {
        var txtMoney = $('<input required data-bind="value:' + options.field + '"/>')
            .appendTo(container)
            .kendoNumericTextBox({
                format: "{0:c}",
                change: function (e) {
                    ComputeSum();
                }
            }).data("kendoNumericTextBox");
    }

    function ComputeSum() {
        var sum = 0;
        $.each(orderDataSource.data(), function (i, o) {
            sum += o.Money;
        });
        $("#lblSum").html(kendo.toString(sum, "c"));
    }
</script>

<script class="grid_orders_dataSource">
    var orderDataSource = new kendo.data.DataSource({
        schema: {
            model: {
                fields: {
                    Bank: { defaultValue: { ID: 0, ProductName: "请选择往来帐号" } },
                    Money: { type: "number", defaultValue: 0 },
                }
            }
        },
        change: function (e) {
            if (e.action == "remove") {
                ComputeSum();
            }
        }
    });
</script>

<script class="grid_orders_config">
    var hasSaved = false;
    $("#grid_orders").kendoGrid({
        columns: orderFields,
        pageable: false,
        toolbar: [{
            name: "create",
            text: "新增一行"
        }, {
            name: "save",
            text: "保存单据"
        }],
        editable: true,
        dataBound: function (e) {
            //让表头居中
            $("th.k-header").attr("style", "text-align:center; font-weight:bold;");
            $("a.k-grid-save-changes").attr("style", "font-weight:bold; float:right");
        },
        dataSource: orderDataSource,
        saveChanges: function (e) {
            if (hasSaved) return; else hasSaved = true;
            var dealOrders = "";
            $("#grid_deal_orders table td input:checked").each(function (i, o) {
                dealOrders += "," + $(o).attr("data-value")
            });
            if (dealOrders.length > 0) {
                dealOrders = dealOrders.substr(1);
                var values = {
                    orderDate: kendo.toString($("#txtNoteDate").data("kendoDatePicker").value(), "yyyy-MM-dd"),
                    customerID: $("#txtCustomer").attr("data-value"),
                    description: $("#txtDescription").val(),
                    method: "@ViewBag.Method",
                    dealOrders: dealOrders,
                    orderItems: []
                };
                values.orderItems = $.map(orderDataSource.data(), function (o) {
                    return {
                        BankID: o.Bank.ID,
                        money: o.Money
                    }
                });
                Whale.Remote.Operation({
                    url: "/Orders/PaidOrder/SaveOrders",
                    parameters: values,
                    success: function (result) {
                        location.href = "/Success/PrintPaidOrder/" + result.OrderNo;
                    },
                    error: function (msg) {
                        hasSaved = false;
                        wndGeneralInformation.content(msg).open();
                    }
                });
            } else {
                hasSaved = false;
                wndGeneralInformation.content("请选择交易单据").open();
            }
        }
    });
</script>
